################################################################################
# Workplace contact and anti-immigration parties
# Author: H Andersson, SH Dehdari
# Description: Statistical analysis
################################################################################

# REMOVES ALL OBJECTS: rm(list = ls())

# We save paths for input and output:
in_output <- "E://ProjData//IntegrationSD//Output//"
in_temp <- "E://ProjData//IntegrationSD//temp//"

out_figure <- "C://Userdata//Shared//Output//IntegrationSD//Figures//" 
out_table <- "C://Userdata//Shared//Output//IntegrationSD//Tables//" 
out_val <- "C://Userdata//Shared//Output//IntegrationSD//Values//"
out_temp <- "C://Userdata//Shared//Output//IntegrationSD//Temp//" 

source("se_comp.R")

# Loading packages
# library(foreign)
# library(miscTools) # for instertRow command
library(readstata13) # To read Stata13 dta-files
# library(dplyr)
library(xtable)
# library(psych)
# library(AER)
# library(multiwayvcov)
library(sandwich)
library(lmtest)
library(ggplot2)


### Importing data: ----
# We import the change in workplace shares between 2006 and 2010, and 2010 and 2014:
yearly_change <- read.dta13(paste(in_temp,"change_share_noneuropean.dta", sep =""))
# We import data for each year between 1990 and 2014:
ylist <- 1990:2014
share_mat <- matrix(NA, ncol= 3, nrow = length(ylist))
for (i in 1:length(ylist)){
  year <- (ylist)[i]
  
  share_mat[i,2] <-  as.numeric(read.dta13(paste(in_temp,"share_noneu_",year,".dta", sep ="")))
  share_mat[i,3] <-  as.numeric(read.dta13(paste(in_temp,"share_noneuWP_",year,".dta", sep ="")))
  
}

share_mat[,1] <- ylist

ggplot_mat <- data.frame(Year = share_mat[,1], Population = share_mat[,2], Workplace = share_mat[,3])


b_plot <- ggplot(ggplot_mat, aes(x = Year))

pdf(paste(out_figure,"yearly_share_noneu.pdf", sep = ""), width = 8, height = 4)
b_plot + geom_line(aes(y = Population), size = 1, linetype = "dashed") + 
         geom_line(aes(y = Workplace), size = 1) +
         theme_bw(base_size = 20) +
         labs(y= "Share (in percent)",x = "Year") + 
         theme(legend.position="none") +
        scale_x_continuous(breaks = c(1990,1995,2000,2005,2010))
dev.off()

# Creating a table for the mean, sd, and min/max of the yearly changes:
c1 <- c("Change in share, 2006-2010","Change in share, 2010-2014")
c2 <- printround(c(mean(yearly_change$non_european2010, na.rm = TRUE),
mean(yearly_change$non_european2014, na.rm = TRUE)),2)
c3 <- printround(c(sd(yearly_change$non_european2010, na.rm = TRUE),
              sd(yearly_change$non_european2014, na.rm = TRUE)),2)
c4 <- round(c(min(yearly_change$non_european2010, na.rm = TRUE),
              min(yearly_change$non_european2014, na.rm = TRUE)))
c5 <- round(c(max(yearly_change$non_european2010, na.rm = TRUE),
              max(yearly_change$non_european2014, na.rm = TRUE)))
c6 <- c(sum(!is.na(yearly_change$non_european2010)),
        sum(!is.na(yearly_change$non_european2014)))

tab_sum <- cbind(c1,c2,c3,c4,c5,c6)

print(xtable(tab_sum), 
      floating = FALSE, only.contents=TRUE, include.rownames = FALSE, 
      include.colnames = FALSE, hline.after=NULL, type = "latex", sanitize.text.function=function(x){x},
      file = paste(out_table, "tab_change_WP.tex", sep = ""))




